/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pa165.languageschoolpersistence.DAOImpl;

import com.pa165.languageschoolpersistence.DAO.StudentDAO;
import com.pa165.languageschoolpersistence.Exceptions.IllegalArgumentPersistenceException;
import com.pa165.languageschoolpersistence.Exceptions.ServiceFailureException;
import com.pa165.languageschoolpersistence.entities.Lecture;
import com.pa165.languageschoolpersistence.entities.Student;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Elena Medvedeva
 */
@Repository("studentDAO")
public class StudentDAOImpl implements StudentDAO{

    private EntityManager em;

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    public Student add(Student student) throws IllegalArgumentPersistenceException {
        if (student == null) {
            throw new IllegalArgumentPersistenceException("Method StudentDAOImpl.add call with Null argument student");
        }
        if (student.getName() == null) {
            throw new IllegalArgumentPersistenceException("Method StudentDAOImpl.add call with Null argument student.name");
        }
        if (student.getLectures() == null) {
            throw new IllegalArgumentPersistenceException("Method StudentDAOImpl.add call with Null argument student.Lecures");
        }
        if (student.getId() != null) {
            Student test = this.get(student.getId());
            if (test == null) {
                return null;
            }
        }
        em.persist(student);
        return student;
    }

    public void remove(Student student) throws ServiceFailureException {
        if ((student == null) || (student.getId() == null)) {
            throw new IllegalArgumentPersistenceException("Method StudentDAOImpl.remove call with Null argument student or its id");
        }
        Student studentToRemove = em.find(Student.class, student.getId());
        em.remove(studentToRemove);
    }

    public Student modify(Student student) throws ServiceFailureException {
        if ((student == null) || (student.getId() == null)) {
            throw new IllegalArgumentPersistenceException("Method StudentDAOImpl.modify call with Null argument student or its id");
        }
        if (student.getName() == null) {
            throw new IllegalArgumentPersistenceException("Method StudentDAOImpl.modify call with Null argument student.name");
        }
        if (student.getLectures() == null) {
            throw new IllegalArgumentPersistenceException("Method StudentDAOImpl.modify call with Null argument student.Lecures");
        }
        em.merge(student);
        return student;

    }

    public Student get(Long id) throws IllegalArgumentPersistenceException {
        if (id == null) {
            throw new IllegalArgumentPersistenceException("Method StudentDAOImpl.get call with Null argument id");
        }
        Student student = null;
        student = em.find(Student.class, id);
        return student;
    }

    public List<Student> getAll() {
        List<Student> result = null;
        result = em.createQuery("from Student", Student.class).getResultList();
        return result;
    }

  

    public List<Student> findByLecture(Lecture lecture) throws  IllegalArgumentPersistenceException {
        if((lecture == null) || (lecture.getId() == null)){
          throw new IllegalArgumentPersistenceException("Method StudentDAOImpl.findByLecture call with Null argument lecture or id:");  
        }
      //  EntityManager em = emf.createEntityManager();
        List<Student> result = null;
      //  try{
      //  em.getTransaction().begin();
        result = em.createQuery( "select s from Student s join fetch s.lectures l where l.id=:id ",Student.class ).setParameter("id", lecture.getId()).getResultList();
        //em.getTransaction().commit();
     //   } finally {
     //        em.close();
     //   }
        return result;
    }

  

    public List<Student> findByName(String name) throws IllegalArgumentPersistenceException  {
         if(name == null){
          throw new IllegalArgumentPersistenceException("Method StudentDAOImpl.get call with Null argument name");  
        }
     //   EntityManager em = emf.createEntityManager();
        List<Student> result;
     //   try{
     //   em.getTransaction().begin();       
        result = em.createQuery( "from Student student where student.name=:name" ).setParameter("name",name).getResultList();
       // em.getTransaction().commit();
     //    } finally {
    //         em.close();
    //    }
        return result;
    }
    
    public Student getByLogin(String login) throws IllegalArgumentPersistenceException {
        if(login == null){
          throw new IllegalArgumentPersistenceException("Method StudentDAOImpl.get call with Null argument name");  
        }
     //   EntityManager em = emf.createEntityManager();
        List<Student> result;
     //   try{
     //   em.getTransaction().begin();       
        result = em.createQuery( "from Student student where student.userName=:login" ).setParameter("login",login).getResultList();
       // em.getTransaction().commit();
     //    } finally {
    //         em.close();
    //    }
        if (!result.isEmpty()) {
            return result.get(0);
        }
        else {
            return null;
        }
    }

    public Lecture enrollStudentToLecture(Lecture lecture, Student student) throws IllegalArgumentPersistenceException {
        if((lecture == null) || (lecture.getId() == null)){
            throw new IllegalArgumentPersistenceException("Method StudentDAOImpl.enrollStudentToLecture call with Null argument lecture or lecture.id");
        }
        if((student == null) || (student.getId() == null)){
            throw new IllegalArgumentPersistenceException("Method StudentDAOImpl.enrollStudentToLecture call with Null argument student or student.id");
        }
     //   EntityManager em = emf.createEntityManager();
      //  try{
      //  em.getTransaction().begin();
         List<Lecture> lecturesTmp = student.getLectures();
         List<Student> studentTmp = lecture.getStudents();
            if (lecturesTmp.contains(lecture) || studentTmp.contains(student)) {
     //           em.close();
                return null;
            }
       
       // lecture.addStudent(student); 
        student.addLecture(lecture);
        em.merge(student);
       // em.merge(lecture);
     //   em.getTransaction().commit();
     //   } finally {
     //     em.close();
     //   }
        return lecture;
    }

    /*public List<Student> findByCourse(Course course) throws IllegalArgumentPersistenceException {
        throw new UnsupportedOperationException("Not supported yet.");
    }*/

    
}
